package com.Jiang.service.User;
import com.Jiang.dao.DateBase;
import com.Jiang.dao.User.UserDao;
import com.Jiang.dao.User.UserDaoImplements;
import com.Jiang.pojo.User;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

public class UserServiceImplements implements UserService {
    public static void main1(String[] args) throws SQLException, ParseException { UserService u=new UserServiceImplements();User user=new User();
       user.setId(null);
       user.setUserCode("ww");
       user.setUserName("ww");
       user.setUserPassword("1111111");
       user.setGender(1);
       user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2000-12-12"));
       user.setPhone("13511111111");
       user.setAddress("月球");
       user.setUserRole(3);
       user.setCreatedBy(1);
       user.setCreationDate(new Date());
       user.setModifyby(null);
       user.setModifyDate(null);
       if(u.addUser(user))System.out.println("成功");
       else System.out.println("失败");
    }
    private UserDao userDao;
    public UserServiceImplements() {
        userDao=new UserDaoImplements();    //多态
    }
    @Override
    public User Login(String userCode,String userPassword) throws SQLException {
        User user=null;
        Connection connection=DateBase.getConnection();
        user=userDao.getLoginUser(connection,userCode);
        if(user!=null&&user.getUserPassword()!=null){
            if(user.getUserPassword().equals(userPassword))
            {
                connection.close();
                return user;
            }
        }
        return null;
    }
    @Override
    public boolean ModifyPassword(Integer id, String userPassword) throws SQLException {
        Connection connection=DateBase.getConnection();
        int result=userDao.ModifyPassword(connection,id,userPassword);
        connection.close();
        return result>0;
    }
    @Override
    public int getUserCount(String userName, int userRole) throws SQLException {
        Connection connection=DateBase.getConnection();
        int count=userDao.getUserCount(connection,userName,userRole);
        connection.close();
        return count;
    }
    @Override
    public Vector<User> getUserList(String userName, int userRole, int currentPageNo, int pageSize) throws SQLException {
        Connection connection=DateBase.getConnection();
        Vector<User> result=userDao.getUserList(connection,userName,userRole,currentPageNo,pageSize);
        connection.close();
        return result;
    }

    @Override
    public boolean addUser(User user) throws SQLException {
        Connection connection=DateBase.getConnection();
        int result=userDao.addUser(connection,user);
        connection.close();
        if(result>0)return true;
        return false;
    }

    @Override
    public boolean FindUserCodeIsExist(String userCode) throws SQLException {
        Connection connection=DateBase.getConnection();
        User user=null;
        user=userDao.getLoginUser(connection,userCode);
        connection.close();
        if(user==null)return false;
        return true;
    }

    @Override
    public User getUserById(int Id) throws SQLException {
       Connection connection=DateBase.getConnection();
        User user = userDao.getUserById(connection, Id);
        connection.close();
        return user;
    }

    @Override
    public boolean modifyUser(User user, int modifyBy) throws SQLException {
        Connection connection=DateBase.getConnection();
        int result=userDao.modifyUser(connection,user,modifyBy);
        connection.close();
        if(result>0)return true;
        return false;
    }

    @Override
    public boolean deluser(int Id) throws SQLException {
        Connection connection=DateBase.getConnection();
        if(userDao.deluser(connection,Id)>0)return true;
        return false;
    }


}
